﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class pay_bus : System.Web.UI.Page
    {
        int finish = 0, finish2 = 0;
        string data_all2="";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["submit"].ToString() == "1")
                {
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    string payment_id = Request.QueryString["data_submit"].ToString();
                    try
                    {
                        var q = from p in db.Table_payments
                                where p.payment_id == payment_id
                                select p;
                        q.ToList().ForEach(c => c.payment_status = true);
                        db.SubmitChanges();

                        int count_chk = 0;
                        string data_all = "";
                        //string input = Request.QueryString["input"];
                        string chk = @"select payment_id,Around_date,Time_time,Price_ticket,tex,Promotion_discount,Category_Discount,
		                            Point_name,payment_enddate,Route_source,Route_destination,Table_Route.platform,Table_Sale.Sale_id,Table_Passenger.Passenger_name
                                    from Table_payment
                                    inner join Table_Sale
                                        on Table_payment.Sale_id = Table_Sale.Sale_id
                                    inner join Table_SaleDetail
                                        on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
                                    inner join Table_Around
                                        on Table_Sale.Around_id = Table_Around.Around_id
                                    inner join Table_Route
                                        on Table_Around.Route_id = Table_Route.Route_id
                                    inner join Table_Promotion
                                        on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
                                    inner join Table_Time
                                        on Table_Around.Time_id = Table_Time.Time_id
                                    inner join Table_Passenger
                                        on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                                    inner join Table_Category
                                        on Table_Passenger.Category_id = Table_Category.Category_id
                                    inner join Table_Point
							            on Table_Sale.Point_id= Table_Point.Point_id
                                    where Table_payment.payment_id = '" + payment_id + "'";
                        try
                        {
                            SqlCommand sqlcomm = new SqlCommand();
                            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                            SqlConnection sqlconn = new SqlConnection(Connection);
                            SqlDataAdapter da = new SqlDataAdapter(chk, sqlconn);
                            DataTable dt = new DataTable();
                            da.Fill(dt);

                            if (dt.Rows.Count <= 0)
                            {
                                count_chk = 0;
                                Response.Redirect("pay_for.aspx?chk_ticket=" + 0);
                            }
                            else
                            {
                                if (DateTime.Parse(dt.Rows[0]["payment_enddate"].ToString()) >= DateTime.Now)
                                {
                                    count_chk = 1;
                                    string data_payment_id = dt.Rows[0]["payment_id"].ToString();
                                    float data_count = float.Parse(dt.Rows.Count.ToString());
                                    string data_Route_source_Route_destination = dt.Rows[0]["Route_source"].ToString() + "-" + dt.Rows[0]["Route_destination"].ToString();
                                    string data_point = dt.Rows[0]["Point_name"].ToString();
                                    string[] stempb = dt.Rows[0]["Around_date"].ToString().Split(' ');
                                    string data_date = stempb[0] + " " + dt.Rows[0]["Time_time"].ToString();
                                    float data_Price = Int32.Parse(dt.Rows[0]["Price_ticket"].ToString());
                                    float data_tex = Int32.Parse(dt.Rows[0]["tex"].ToString());
                                    float data_promotion = float.Parse(dt.Rows[0]["Promotion_discount"].ToString());
                                    float data_user = float.Parse(dt.Rows[0]["Category_Discount"].ToString());
                                    float data_sumPro = (data_Price * (data_promotion + data_user)) / 100;
                                    float data_sumPro2 = ((data_Price - data_sumPro) + data_tex);
                                    float data_sumall = (data_count * ((data_Price - data_sumPro) + data_tex));
                                    string data_platform = dt.Rows[0]["platform"].ToString();
                                    string data_name = dt.Rows[0]["Passenger_name"].ToString();
                                    string data_seat = "";
                                    int cchk = 0;
                                    for (int i = 0; i < dt.Rows.Count; i++)
                                    {
                                        string chk_seat = @"select *
                                                    from Table_SaleDetail
                                                    where Sale_id ='" + dt.Rows[i]["Sale_id"].ToString() + "'";
                                        SqlCommand sqlcomm3 = new SqlCommand();
                                        string Connection3 = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                                        SqlConnection sqlconn3 = new SqlConnection(Connection);
                                        SqlDataAdapter da3 = new SqlDataAdapter(chk_seat, sqlconn);
                                        DataTable dt3 = new DataTable();
                                        da3.Fill(dt3);
                                        try
                                        {
                                            if (dt3.Rows.Count <= 0)
                                            {
                                                
                                            }
                                            else
                                            {
                                                for (int k = 0; k < dt3.Rows.Count; k++)
                                                {
                                                    if (cchk == 0)
                                                    {
                                                        data_seat = dt3.Rows[k]["Seat_no"].ToString();
                                                        cchk++;
                                                    }
                                                    else
                                                    {
                                                        data_seat += "," + dt3.Rows[k]["Seat_no"].ToString();
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            
                                            throw;
                                        }                                        
                                    }
                                    data_all2 = data_payment_id + "*" + data_count.ToString() + "*" + data_Route_source_Route_destination + "*" + data_date + "*" + data_sumall.ToString() + "*" + data_point + "*" + data_sumPro2 + "*" + data_seat + "*" + data_platform + "*" + data_name;
                                    finish2 = 1;
                                    Response.Redirect("WebFormtrstt.aspx?print_ticket=" + 1 + "&data_all=" + data_all);
                                }
                                else
                                {
                                    count_chk = 0;
                                    Response.Redirect("WebFormtrstt.aspx?print_ticket=" + 0);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            if (count_chk == 1)
                            {

                                Response.Redirect("pay_for.aspx?chk_ticket=" + 1 + "&data_all=" + data_all);
                            }
                            else if (count_chk == 0)
                            {
                                Response.Redirect("pay_for.aspx?chk_ticket=" + 0);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Response.Redirect("pay_for.aspx?chk_ticket=" + 4);
                    }
                    finish = 1;
                    Response.Redirect("pay_for.aspx?chk_ticket=" + 4);
                }
            }
            catch (Exception)
            {
                if (finish2 ==1)
                {
                    Response.Redirect("WebFormtrstt.aspx?&data_all=" + data_all2);
                }
                if (finish ==0 )
                {
                    int count_chk = 0;
                    string data_all = "";
                    string input = Request.QueryString["input"];
                    string chk = @"select payment_id,Route_source,Around_date,Time_time,Price_ticket,tex,Promotion_discount,Category_Discount,Point_name,payment_enddate,Route_source,Route_destination
                        from Table_payment
                        inner join Table_Sale
                            on Table_payment.Sale_id = Table_Sale.Sale_id
                        inner join Table_SaleDetail
                            on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
                        inner join Table_Around
                            on Table_Sale.Around_id = Table_Around.Around_id
                        inner join Table_Route
                            on Table_Around.Route_id = Table_Route.Route_id
                        inner join Table_Promotion
                            on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
                        inner join Table_Time
                            on Table_Around.Time_id = Table_Time.Time_id
                        inner join Table_Passenger
                            on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                        inner join Table_Category
                            on Table_Passenger.Category_id = Table_Category.Category_id
                        inner join Table_Point
							on Table_Sale.Point_id= Table_Point.Point_id
                        where Table_payment.encode = '" + input + "'";
                    try
                    {
                        SqlCommand sqlcomm = new SqlCommand();
                        string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        SqlDataAdapter da = new SqlDataAdapter(chk, sqlconn);
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        if (dt.Rows.Count <= 0)
                        {
                            count_chk = 0;
                            Response.Redirect("pay_for.aspx?chk_ticket=" + 0);
                        }
                        else
                        {
                            if (DateTime.Parse(dt.Rows[0]["payment_enddate"].ToString()) >= DateTime.Now)
                            {
                                count_chk = 1;
                                string data_payment_id = dt.Rows[0]["payment_id"].ToString();
                                float data_count = float.Parse(dt.Rows.Count.ToString());
                                string data_Route_source_Route_destination = dt.Rows[0]["Route_source"].ToString() + "-" + dt.Rows[0]["Route_destination"].ToString();
                                string data_point = dt.Rows[0]["Point_name"].ToString();
                                string[] stempb = dt.Rows[0]["Around_date"].ToString().Split(' ');
                                string data_date = stempb[0] + " " + dt.Rows[0]["Time_time"].ToString();
                                float data_Price = Int32.Parse(dt.Rows[0]["Price_ticket"].ToString());
                                float data_tex = Int32.Parse(dt.Rows[0]["tex"].ToString());
                                float data_promotion = float.Parse(dt.Rows[0]["Promotion_discount"].ToString());
                                float data_user = float.Parse(dt.Rows[0]["Category_Discount"].ToString());
                                float data_sumPro = (data_Price * (data_promotion + data_user)) / 100;
                                float data_sumall = (data_count * ((data_Price - data_sumPro) + data_tex));
                                data_all = data_payment_id + "*" + data_count.ToString() + "*" + data_Route_source_Route_destination + "*" + data_date + "*" + data_sumall.ToString() + "*" + data_point;
                                Response.Redirect("pay_for.aspx?chk_ticket=" + 1 + "&data_all=" + data_all);
                            }
                            else
                            {
                                count_chk = 0;
                                Response.Redirect("pay_for.aspx?chk_ticket=" + 0);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (count_chk == 1)
                        {

                            Response.Redirect("pay_for.aspx?chk_ticket=" + 1 + "&data_all=" + data_all);
                        }
                        else if (count_chk == 0)
                        {
                            Response.Redirect("pay_for.aspx?chk_ticket=" + 0);
                        }
                    }
                }
                else
                {
                    finish = 0;
                    Response.Redirect("pay_for.aspx?chk_ticket=" + 4);
                }                               
            }            
        }
    }
}